﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Survival.Plants;
using Survival.Services;
using Survival.AI;
using Microsoft.Xna.Framework.Graphics;

namespace Survival.Animals
{
    public class Cow : Animal
    {
        private TaskScheduler taskScheduler = new TaskScheduler();
        private HerdMover cowMover;
        private const float cowSpeed = 100f;
        private Stopwatch stopwatch = new Stopwatch();

        public override Vector2 Position { get { return cowMover.Position; } }
        public override float Rotation { get { return cowMover.Rotation; } }
        public override string SpriteName { get { return "Cow"; } }

        public Cow(Vector2 position,AnimalServices  animalServices): base(animalServices)
        {
            cowMover = new HerdMover(position, cowSpeed);
            stopwatch.Start();
        }

        public override void Update(GameTime gameTime)
        {
            if (stopwatch.Elapsed.Seconds > 2)
            {
                stopwatch.Restart();

                List<PlantData> grasslist = AnimalServices.WorldQuery.GetVisiblePlants(this, 300);
                if (grasslist.Count > 0)
                {
                    PlantData bestDistance = grasslist[0];
                    foreach (PlantData pdata in grasslist)
                    {
                        if (pdata.DistanceSquared < bestDistance.DistanceSquared)
                            bestDistance = pdata;
                    }
                    cowMover.MoveTo(bestDistance.Position);
                }
            }
            cowMover.UpdateMovement(gameTime);
        }
    }
}
